package com.hbuzzs.springcloud.entities;

import com.google.common.collect.ImmutableMap;


/**
 * 默认返回枚举
 *
 * @author tongmingjian
 */
public enum MsgEnum {

    /**
     * 请求成功
     */
    SUCCESS(200, "请求成功！"),

    /**
     * 权限不足
     */
    AUTH_ERROR(400, "权限不足！"),

    /**
     * 请求成功
     */
    UNAUTHORIZED(401, "请重新进行身份认证！"),

    /**
     * 服务器理解请求客户端的请求，但是拒绝执行此请求
     */
    FORBIDDEN(403, "拒绝执行此请求！"),

    /**
     * 服务器无法根据客户端的请求找到资源（网页）。通过此代码，网站设计人员可设置"您所请求的资源无法找到"的个性页面
     */
    NOT_FOUND(404, "您所请求的资源无法找到！"),

    /**
     * 服务器异常
     */
    SERVER_ERROR(500, "服务器异常！"),

    /**
     * 未知错误
     */
    UN_KNOW_ERROR(501, "未知错误！"),


    ;
    private final int code;

    private final String message;

    private static final ImmutableMap<Integer, MsgEnum> CACHE;

    static {
        final ImmutableMap.Builder<Integer, MsgEnum> builder = ImmutableMap.builder();
        for (MsgEnum statusCode : values()) {
            builder.put(statusCode.code(), statusCode);
        }
        CACHE = builder.build();
    }

    MsgEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static MsgEnum valueOfCode(int code) {
        final MsgEnum status = CACHE.get(code);
        if (status == null) {
            throw new IllegalArgumentException("No matching constant for [" + code + "]");
        }
        return status;
    }

    public int code() {
        return code;
    }

    public String message() {
        return message;
    }
}